package com.xtedu.examproject.controller;

import com.xtedu.examproject.entity.Course;
import com.xtedu.examproject.entity.ExamQuestion;
import com.xtedu.examproject.entity.Examination;
import com.xtedu.examproject.entity.User;
import com.xtedu.examproject.service.CourseService;
import com.xtedu.examproject.service.ExaminationService;
import com.xtedu.examproject.util.RedisUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/stuexammanage/*")
public class StudentExamServlet extends HttpServlet {
    private ExaminationService examinationService;
    private CourseService courseService;

    @Override
    public void init() throws ServletException {
        examinationService = new ExaminationService();
        courseService = new CourseService();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
//        String pathInfo = req.getPathInfo();
        try {
            getExamination(req, resp);
        } catch (Exception e) {
            throw new ServletException("处理请求失败", e);
        }
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String pathInfo = req.getPathInfo();

        try {
            // 处理 null 或空路径
            if (pathInfo == null || pathInfo.equals("/") || pathInfo.isEmpty()) {
                getExamination(req, resp);
                return;
            }

            switch(pathInfo) {
                case "startExam": startExam(req, resp);
            }
        } catch (Exception e) {
            throw new ServletException("处理请求失败", e);
        }
    }

    private void startExam(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String examId = req.getParameter("examId");
        List<ExamQuestion> examQuestions = RedisUtil.getObject("exam:" + examId, List.class);
        if(examQuestions == null) {
            throw new RuntimeException("试卷暂未初始化");
        }
        req.setAttribute("examQuestions", examQuestions);
        req.getRequestDispatcher("/WEB-INF/views/examTalking.jsp").forward(req, resp);
    }

    /**
     * 获取考试信息
     */
    public void getExamination(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        resp.setContentType("text/html;charset=UTF-8");
        HttpSession session = req.getSession();
        int stuId = (int) session.getAttribute("stuId");
        int courseId = courseService.findCourseIdByStudentId(stuId);
        List<Examination> examList = RedisUtil.getObject("examination:" + courseId, List.class);
        if (examList == null) {
            try {
                examList = examinationService.getExaminationByCourseId(courseId);
                RedisUtil.setObject("exam:" + courseId, examList, 60 * 60 * 24);
            } catch (Exception e) {
                //记录日志
                throw new RuntimeException(e);
            }
        }
        req.setAttribute("examList", examList);
        req.getRequestDispatcher("/WEB-INF/views/stuexam.jsp").forward(req, resp);
    }


}
